package com.oauth.application.client.ui;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.MouseOutEvent;
import com.google.gwt.event.dom.client.MouseOverEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Widget;
import com.oauth.application.client.beans.ApplicationClientBean;
import com.oauth.application.client.rpc.ApplicationRemoteServiceAsync;

public class ApplicationItem extends Composite {

	private static ApplicationItemUiBinder uiBinder = GWT.create(ApplicationItemUiBinder.class);

	interface ApplicationItemUiBinder extends UiBinder<Widget, ApplicationItem> {
	}

	private ApplicationClientBean mApplication;

	@UiField
	Image image;

	@UiField
	Label applicationName;

	@UiField
	HorizontalPanel itemPanel;

	@UiField
	HorizontalPanel panelForRemoveButton;

	public ApplicationItem(final ApplicationClientBean pApplication,
			ApplicationRemoteServiceAsync applicationUnSubscribeRemoteServiceAsync) {
		mApplication = pApplication;

		initWidget(uiBinder.createAndBindUi(this));

		image.setUrl(pApplication.getIconImageUrl());

		applicationName.setText(pApplication.getName());
		if (applicationUnSubscribeRemoteServiceAsync != null) {
			panelForRemoveButton.add(new UnSubscribeApplicationButton(pApplication,
					applicationUnSubscribeRemoteServiceAsync));
		} else {
			panelForRemoveButton.setVisible(false);
		}
	}

	@UiHandler("applicationName")
	public void onMouseOver(MouseOverEvent event) {
		itemPanel.addStyleName("highlight-cursor");
	}

	@UiHandler("applicationName")
	public void onMouseOut(MouseOutEvent event) {
		itemPanel.removeStyleName("highlight-cursor");
	}

	@UiHandler("applicationName")
	public void onClick(ClickEvent event) {
		Window.Location.assign(mApplication.getAddUrl());
	}

}
